Taylor series

Understanding and visualizing 1D and 2D taylor series.
Author

Vannsh Jani

Published

August 14, 2023

What is the Taylor series?

The Taylor series is a mathematical representation of a function as an infinite sum of terms. It is mainly used to approximate a non-polynomial function in terms of polynomials of degree n. Higher the degree n, the better the approximation.

The general form of the Taylor series for the function \(f(x)\) centered about the point a is given by:

\[ f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)(x-a)^2}{2!} + \frac{f'''(a)(x-a)^3}{3!} +... \]

Let’s say we take the function \(y = sin(x)\)

import math
import matplotlib.pyplot as plt

x = [i * 0.1 for i in range(-63, 64)]
y = [math.sin(xi) for xi in x]


plt.plot(x, y,)
plt.title('Plot of y = sin(x)')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.show()

Let’s choose a=0 and let’s go up to n=5, then using the Taylor series we can write,

\[ sin(x) = 0 + cos(0)(x)+\frac{-sin(0)x^2}{2!}+\frac{-cos(0)x^3}{3!}+\frac{sin(0)x^4}{4!}+\frac{cos(0)x^5}{5!} \]

where \(f(x)=sin(x),f'(x)=cos(x),f''(x)=-sin(x),f'''(x)=-cos(x),f''''(x)=sin(x),and f'''''(x)=cos(x)\)

Therefore,

\(sin(x) = x-\frac{x^3}{3!}+\frac{x^5}{5!}\)

import math
import matplotlib.pyplot as plt

x = [i * 0.1 for i in range(-33, 34)]
y = [xi-((xi**3)/6)+((xi**5)/120) for xi in x]
y1 = [math.sin(xi) for xi in x]
plt.figure()
plt.plot(x, y,label="y=x-(x^3)/3!+(x^5)/5!")
plt.plot(x,y1,label="y=sinx")
plt.title('Approximating sin(x) using y = x - (x^3)/3! + (x^5)/5!')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc="upper left")
plt.show()

We can see above that, the polynomial function is providing us a good approximation of the sine function but only for values close to zero (as we selected a to be 0). For other values the approximation is inaccurate.

When we truncate the infinite Taylor series to a polynomial of degree n, we call it the Taylor polynomial of degree n. The higher the value of n, the more accurate our approximation is.

Taylor series for bivariate functions

We can generalize the Taylor series for functions depending on multiple variables.

Let’s discuss the Taylor series for bivariate functions. The formula is given by,

\[ f(x,y)= f(a,b)+\frac{\partial f}{\partial x}(x-a)+\frac{\partial f}{\partial y}(y-b)+\frac{\partial^2 f}{\partial x^2}\frac{(x-a)^2}{2!}+\frac{\partial^2 f}{\partial y^2}\frac{(y-b)^2}{2!}+... \]

Here the approximation of the function takes place near the point,\((a,b)\), and the partial derivatives are calculated at the point \((a,b)\).

Let’s take an example.

\[ f(x,y)=x^2+y^2 \]

We can try to approximate this function using the 2D Taylor series around the point \((1,1)\) and let’s choose the value of n as 2.

Let’s first plot \(f(x,y)\).

import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(-5,5,100)
y1 = np.linspace(-5,5,100)
X1,Y1 =np.meshgrid(x1,y1)
Z_true=X1**2 + Y1**2

fig = plt.figure()
ax = plt.axes(projection="3d")
ax.plot_surface(X1,Y1,Z_true,cmap="plasma")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.view_init(elev=20, azim=30)
plt.show()

Let us try to approximate this function using the Taylor series with a=b=1 and n =2.

\[ \begin{equation} \begin{split} f(x,y)&=2+2(x-1)+2(y-1)+(x-1)^2+(y-1)^2\\ f(x,y)&=x^2+y^2 \end{split} \end{equation} \]

which is the true value indeed. Hence, the Taylor series exactly approximates \(f(x,y)\) when centered about the point \((1,1)\).

You can visualize the approximations of the Taylor series in the link given below.

https://vannshtaylor.streamlit.app/